import {Global, Module, ValidationPipe} from "@nestjs/common";
import {DatabaseModule} from "./database/database.module";
import {APP_FILTER, APP_GUARD, APP_INTERCEPTOR, APP_PIPE} from "@nestjs/core";
import {ExceptionsFilter} from "../../common/filters/exception.filter";
import {ResponseTransformInterceptor} from "../../common/interceptors/responseTransfrom.interceptor";
import {AuthModule} from "./auth/auth.module";
import {JwtAuthGuard} from "../../common/guards/jwtAuth.guard";
import {LoggerModule} from "./logger/logger.module";
import {MailModule} from "./mail/mail.module";
import {ScheduleModule} from "@nestjs/schedule";
import {UserActionModule} from "./userAction/userAction.module";
import {UserScoreModule} from "./userScore/userScore.module";

@Global()
@Module({
    imports: [
        DatabaseModule,
        AuthModule,
        LoggerModule,
        MailModule,
        UserActionModule,
        UserScoreModule,
        ScheduleModule.forRoot()
    ],
    providers: [
        {
            /* 返回数据封装 */
            provide: APP_INTERCEPTOR,
            useClass: ResponseTransformInterceptor
        },
        {
            /* 全局异常拦截器 */
            provide: APP_FILTER,
            useClass: ExceptionsFilter
        },
        {
            /* jwt全局守卫 */
            provide: APP_GUARD,
            useClass: JwtAuthGuard
        },
        {
            /* 全局参数校验管道 */
            provide: APP_PIPE,
            useValue: new ValidationPipe({
                whitelist: true, // 启用白名单，dto中没有声明的属性自动过滤
                transform: true // 自动类型转换
            })
        }
    ]
})
export class CommonModule {
}
